package com.wss.lsl.lucene.demo.shipin.index.tika;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.logging.SimpleFormatter;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.ContentHandler;

import com.chenlb.mmseg4j.analysis.MMSegAnalyzer;
import com.wss.lsl.lucene.demo.shipin.advanced.query.FileIndexUtils;

public class IndexUtils {

	private static String indexFile = "E:/test/doc/申请.doc";
	private static Analyzer analyzer = new MMSegAnalyzer(
			"G:\\important\\apache\\mmseg\\mmseg4j-1.8.5\\data");

	public static void indexNormal() {
		try {
			String indexDir = "d:/lucene/index/file1";
			Directory directory = FSDirectory.open(new File(indexDir));
			IndexWriter writer = new IndexWriter(directory,
					new IndexWriterConfig(Version.LUCENE_35, analyzer));
			writer.deleteAll();
			Document doc = new Document();
			doc.add(new Field("content", new FileReader(new File(indexFile))));
			writer.addDocument(doc);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void indexByTika() {
		try {
			String indexDir = "d:/lucene/index/file2";
			String content = fileToString(new File(indexFile));
			Directory directory = FSDirectory.open(new File(indexDir));
			IndexWriter writer = new IndexWriter(directory,
					new IndexWriterConfig(Version.LUCENE_35, analyzer));
			writer.deleteAll();
			Document doc = new Document();
			doc.add(new Field("content", new StringReader(content)));
			writer.addDocument(doc);
			writer.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// Tika的第二种使用方式：调用更简单，fileToString的调用方式效率更高
	private static String tikaToString(File file) {
		try {
			Metadata metadata = new Metadata();
			Tika tika = new Tika();
			String result = tika.parseToString(new FileInputStream(file), metadata);
			metadata.getInt(Metadata.PAGE_COUNT);
			System.out.println(result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return null;
	}
	
	private static String fileToString(File file) {
		InputStream is = null;
		try {
			Parser parser = new AutoDetectParser();
			ContentHandler handler = new BodyContentHandler();
			Metadata metadata = new Metadata();
			is = new FileInputStream(file);
			ParseContext context = new ParseContext();
			context.set(Parser.class, parser);
			parser.parse(is, handler, metadata, context);
			String result = handler.toString();
			System.out.println(result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	// 高亮显示
	public static void lighter(){
		String txt = "我爱伟大天安门，天安门上彩旗飞，伟大领袖毛主席，指引我们向前进，向前进！！！！";
		
		try {
			Query query = new QueryParser(Version.LUCENE_35, "f", analyzer).parse("毛主席 天安门");
			QueryScorer scorer = new QueryScorer(query);
			Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
			Highlighter highlighter = new Highlighter(scorer);
			highlighter.setTextFragmenter(fragmenter);
			
			String result = highlighter.getBestFragment(analyzer, "f", txt);
			
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 创建索引后用lukeall查看索引创建情况
	public static void main(String[] args) {

		IndexUtils.indexNormal();
		IndexUtils.indexByTika();
		long start = System.currentTimeMillis();
		tikaToString(new File(indexFile));
		System.out.println("tikaToString: "+(System.currentTimeMillis() - start));
		start = System.currentTimeMillis();
		fileToString(new File(indexFile));
		System.out.println("fileToString: "+(System.currentTimeMillis() - start));
		System.out.println("index done!");
		
		lighter();
		
		/*try {
			IndexSearcher searcher = new IndexSearcher(IndexReader.open(FileIndexUtils.getDirectory()));
			QueryParser parser = new QueryParser(Version.LUCENE_35, "", analyzer);
			parser.parse("");
			Query query = new TermQuery(new Term("", ""));
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

}
